export default{
  // 开启命名空间
  namespaced: true,
  
  state: () => ({
    // 3.读取本存储的收货地址数据，初始化address对象
    address: JSON.parse(uni.getStorageSync('address') || '{}'),
    // 登录成功之后的token字符串
    token: uni.getStorageSync('token') || '',
    // 用户的基本信息
    userInfo: JSON.parse(uni.getStorageSync('userInfo') || '{}'),
    
    // 重定向的object对象{opentype,from}
    redireactInfo: null
  }),
  
  mutations: {
    // 更新收货地址
    updateAddress(state,address){
      state.address = address
      // console.log(address)
      // 2.通过commit调用saveAddressToStorage方法
      this.commit('m_user/saveAddressToStorage')
    },
    
    // 1.定义将address持久化存储到本地
    saveAddressToStorage(state){
      uni.setStorageSync('address',JSON.stringify(state.address))
    },
    
    // 更新用户的基本信息
    updateUserInfo(state,userInfo){
      state.userInfo = userInfo
      this.commit('m_user/saveUserInfoToStorage')
    },
    
    // 将useInfo持久化存储到本地
    saveUserInfoToStorage(state){
      uni.setStorageSync('userInfo',JSON.stringify(state.userInfo))
    },
    
    // 更新token字符串
    updateToken(state,token){
      state.token = token
      this.commit('m_user/saveTokenToStorage')
    },
    // 将token持久化存储到本地
    saveTokenToStorage(state){
      uni.setStorageSync('token',state.token)
    },
    
    // 更新重定向的信息对象
    updateRedireactInfo(state,info){
      state.redireactInfo = info
      // console.log(state.redireactInfo)
    }
    
    // // 清除state里的token
    // cleanToken(state){
    //   state.token = ''
    //   this.commit('m_user/cleanStorageToken')
    // },
    // cleanStorageToken(){
    //   uni.setStorageSync('token','')
    // }
  },
  
  getters: {
    addstr(state){
      if(!state.address.provinceName) return ''
      return state.address.provinceName + state.address.cityName + state.address.countyName + state.address.detailInfo
    }
  }
}